/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util.jQgrid;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author CHANWUT
 */
public class JQGridFormatterUtil {

    private static final Log logger = LogFactory.getLog(JQGridFormatterUtil.class);

    public static String getJSON(int currentPageNo,
            int totalRecords, Set objectsToBeAdded,
            List<String> orderedPropertyNames) {

        Integer pages = 0;

        if (totalRecords % 50 > 0) {
            pages = (totalRecords / 50) + 1;
        } else {
            pages = (totalRecords / 50);
        }

        JQGridContainer container = new JQGridContainer();
        container.setPage(currentPageNo);
        container.setTotal(pages);
        container.setRecords(totalRecords);
        List rows = new ArrayList();
        if (!objectsToBeAdded.isEmpty()) {
            for (Object obj : objectsToBeAdded) {
                JQGridRow row = new JQGridRow();
                row.setId(new Long(getPropertvalue(obj, "id")));
                List cells = new ArrayList();
                for (String propertyName : orderedPropertyNames) {
                    cells.add(getPropertvalue(obj, propertyName));
                }

                row.setCell(cells);
                rows.add(row);
            }
        }
        container.setRows(rows);
        return JSONUtil.convertToJSON(container);
        //For
        //JSONSerializer serializer = new JSONSerializer();
        //String jsonResult = serializer.exclude("*.class").deepSerialize(json);
    }

    /**
     * override getJSON : use when query dynamic column that cannot provide by Entity directly
     * @param currentPageNo
     * @param totalRecords
     * @param rows
     * @return
     */
    public static String getJSON(int currentPageNo,
            int totalRecords,
            List rows) {

        Integer pages = 0;

        if (totalRecords % 50 > 0) {
            pages = (totalRecords / 50) + 1;
        } else {
            pages = (totalRecords / 50);
        }

        JQGridContainer container = new JQGridContainer();
        container.setPage(currentPageNo);
        container.setTotal(pages);
        container.setRecords(totalRecords);
        container.setRows(rows);
        return JSONUtil.convertToJSON(container);
    }
    /**
     * override getJSON : use when query dynamic column that cannot provide by Entity directly
     * @param currentPageNo
     * @param limitRecords
     * @param totalRecords
     * @param rows
     * @return
     */
    public static String getJSON(int currentPageNo,
            int limitRecords,
            int totalRecords,
            List rows) {

        Integer pages = 0;

        if (totalRecords % limitRecords > 0) {
            pages = (totalRecords / limitRecords) + 1;
        } else {
            pages = (totalRecords / limitRecords);
        }

        JQGridContainer container = new JQGridContainer();
        container.setPage(currentPageNo);
        container.setTotal(pages);
        container.setRecords(totalRecords);
        container.setRows(rows);
        return JSONUtil.convertToJSON(container);
    }

    private static String getPropertvalue(Object bean, String propName) {
        String val = null;
        try {
            val = ObjectUtils.toString(PropertyUtils.getProperty(bean, propName));
        } catch (IllegalAccessException e) {
            logger.error(e);
        } catch (InvocationTargetException e) {
            logger.error(e);
        } catch (NoSuchMethodException e) {
            logger.error(e);
        }
        return val;
    }
}
